<?php
class AdminTourCategoryController extends MyZend_Controller_Action {
	protected $_arrParam;
	
	protected $_actionMain;
	
	protected $_currentController;
	
	protected $_namespace;
	
	protected $_paginator = array(
			'itemCountPerPage' => 5,
			'pageRange' => 4
	);
	
	//protected $_errors = array();
	
	public function init() {
	
		$this->_arrParam          = $this->_request->getParams();
		$this->_currentController = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'];
	
		$this->_actionMain        = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'] . '/index';
		
		// filter session
		$this->_namespace         = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
			$ssFilter->col   = 'c.id';
			$ssFilter->order = 'DESC';
		}
		
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 		= $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 		= $ssFilter->order;
		
		// paginator
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator']    = $this->_paginator;
	
		// render to view
		$this->view->arrParam 		   = $this->_arrParam;
		$this->view->actionMain 	   = $this->_actionMain;
		$this->view->currentController = $this->_currentController;
	
		$this->_helper->layout()->disableLayout();
		$template_path = TEMPLATE_PATH . '/admin/main';
		$this->loadTemplate($template_path);
	}
	
	public function indexAction() {
		$this->view->Title = 'Tour :: Category manager :: List';
		$this->view->headTitle($this->view->Title, true);
		$tblCategory = new Default_Model_TourCategory();
		$this->view->Items = $tblCategory->listItem($this->_arrParam, array('task' => 'admin-list'));
		
		$paginator = new MyZend_Paginator();
		$totalItem = $tblCategory->countItem($this->_arrParam);
		$this->view->paginator = $paginator->creatPaginator($totalItem, $this->_paginator);
			
	}
	
	// filter data post
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		// sort
		if($this->_arrParam['type'] == 'order') {
			$ssFilter->col   = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
	
		// seach
		if($this->_arrParam['type'] == 'search'){
			if($this->_arrParam['key'] == 1){
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			}else{
				$ssFilter->keywords = '';
			}
		}
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	// change status
	public function statusAction() {
		$tblCategory = new Default_Model_TourCategory();
		$tblCategory->changeStatus($this->_arrParam);
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	// order items
	public function sortAction() {
		if($this->_request->isPost()){
			$cid   = $this->_arrParam['cid'];
			$order = $this->_arrParam['order'];
				
			if(count($cid) > 0) {
				$validator = new Zend_Validate();
				$validator->addValidator(new Zend_Validate_NotEmpty(), true);
				$validator->addValidator(new Zend_Validate_Int(), true);
				$validator->addValidator(new Zend_Validate_GreaterThan(0), true);
	
				foreach ($cid as $key => $value) {
					if(!$validator->isValid($order[$value])) {
						$messages        = $validator->getMessages();
						// unset value order not validator
						unset($this->_arrParam['cid'][$key]);
					}
				}
	
	
				$tblCategory = new Default_Model_TourCategory();
				$tblCategory->sortItems($this->_arrParam);
			}
				
			$this->_redirect($this->_actionMain);
		}
	}
	
    // delete item
	public function deleteAction() {
		$this->view->Title = 'Tour :: Category manager :: Delete';
		$this->view->headTitle($this->view->Title,true);
		if($this->_request->isPost()){
			$tblCategory = new Default_Model_TourCategory();
		    $tblCategory->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
			$this->_redirect($this->_actionMain);
		}
	}
	
	// delete multi items
	public function multiDeleteAction() {
		if($this->_request->isPost()){
			$tblCategory = new Default_Model_TourCategory();
			$tblCategory->deleteItem($this->_arrParam, array('task'=>'admin-multi-delete'));
			$this->_redirect($this->_actionMain);
		}
	}
	
	// add new item
	public function addAction() {
		$this->view->Title = 'Tour :: Category manager :: Add New';
		$this->view->headTitle($this->view->Title,true);
		$tblSubmenu   = new Default_Model_SubMenu();
		$arrsOptions  = $tblSubmenu->itemInSelectbox();
		$this->view->options = $arrsOptions;
		
		if($this->_request->isPost()) {
		    $validator = new Default_Form_ValidateCategoryTour($this->_arrParam);
			if($validator->isError()) {
				$this->view->messageError = $validator->getMessageError();
				$this->view->data         = $validator->getData();
			}else {
				$tblCategory = new Default_Model_TourCategory();
				$tblCategory->saveItem($this->_arrParam, array('task'=>'admin-add'));
				$this->_redirect($this->_actionMain);
			}  
				
		}
	}
	
	// view detail item
	public function infoAction() {
		$this->view->Title = 'Tour :: Category manager :: Information';
		$this->view->headTitle($this->view->Title,true);
		$tblCategory              = new Default_Model_TourCategory();
		$this->view->Item         = $tblCategory->getItem($this->_arrParam, array('task' => 'admin-info'));
		
	}
	
	// edit item
	public function editAction() {
		$this->view->Title = 'Tour :: Category manager :: Edit';
		$this->view->headTitle($this->view->Title,true);
		$tblCategory      = new Default_Model_TourCategory();
		$this->view->Item = $tblCategory->getItem($this->_arrParam, array('task'=>'admin-edit'));
		
		$tblSubmenu    = new Default_Model_SubMenu();
		$arrsOptions   = $tblSubmenu->itemInSelectbox();
		$this->view->options = $arrsOptions;
	
		if($this->_request->isPost()) {
			$validator = new Default_Form_ValidateCategoryTour($this->_arrParam);
			if($validator->isError()) {
				$this->view->messageError = $validator->getMessageError();
			}else {
				$tblCategory->saveItem($this->_arrParam, array('task' => 'admin-edit'));
				$this->_redirect($this->_actionMain);
			}
				
		}
	}
}